home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / challenge / 12.08-Aug96 / MazeTestCode.sit / MazeTestCode ƒ / MazeTestMain.c < prev    next >
C/C++ Source or Header  |  1996-07-21  |  4KB  |  130 lines

  1. #include "Maze.h"
  2. #include <stdio.h>
  3. #include <stddef.h>
  4. #include "timer.h"
  5.  
  6. #define DBG 0
  7.  
  8. static enum {rock=0,empty=1} CellType;
  9.  
  10. /* Insert maze files you wish to test here */
  11. /* See ReadMaze for file format if you wish to create your own */
  12. static char *testFiles[] = {"maze1.dat",
  13.                             "maze2.dat",
  14.                             "maze3.dat"};
  15. #define kNumFiles 3
  16.  
  17. /* Some defines for the callback to read the maze data structure */
  18. #define MazeVal(maze,x,y,z) *(maze + (x) + (y)*gxSize + (z)*gxSize*gySize)
  19.  
  20. #define OutOfBounds(x,y,z) \
  21.         ((x<0)  || (y<0)  || (z<0)  || (x>=gxSize)   || (y>=gySize)   || (z>=gzSize) )
  22.  
  23. #define OnBoundary(x,y,z) \
  24.         ((x==0) || (y==0) || (z==0) || (x==gxSize-1) || (y==gySize-1) || (z==gzSize-1) )
  25.  
  26. static char * ReadMaze(FILE *fileName,
  27.     long *xSize,long *ySize,long *zSize,long *xStart,long *yStart,long *zStart)
  28. {
  29.     size_t numread;
  30.     char *maze;
  31.     numread = fread(xSize,sizeof(long),1,fileName);
  32.     numread = fread(ySize,sizeof(long),1,fileName);
  33.     numread = fread(zSize,sizeof(long),1,fileName);
  34.     numread = fread(xStart,sizeof(long),1,fileName);
  35.     numread = fread(yStart,sizeof(long),1,fileName);
  36.     numread = fread(zStart,sizeof(long),1,fileName);
  37.     maze = NewPtr(*xSize**ySize**zSize);
  38.     numread = fread(maze,1,*xSize**ySize**zSize,fileName);
  39.     return maze;
  40. }    
  41.  
  42. /*
  43.  * Private storage used by callback to check validity of moves
  44.  *  gMaze stores the maze, with MazeVal(x,y,z)==empty for empty cells
  45.  *  gxSize,gySize,gzSize are the maze dimensions
  46.  *  gxPos,gyPos,gzPos is current position
  47. */
  48. static long gxPos,gyPos,gzPos;
  49. static long gxSize,gySize,gzSize;
  50. static char *gMaze;
  51.  
  52. /* Callback */
  53. static Boolean /* found exit */ MakeAMove(
  54.     long xMove,        /* amount of attempted move in x direction(1,0,-1) */
  55.     long yMove,        /* amount of attempted move in y direction(1,0,-1) */
  56.     long zMove,        /* amount of attempted move in z direction (1,0, -1) */
  57.     long *newXPos,    /* new x position after attempted move */
  58.     long *newYPos,    /* new y position after attempted move */
  59.     long *newZPos    /* new y position after attempted move */
  60. ){
  61.     static long x,y,z;
  62.     Boolean result;
  63.     *newXPos = gxPos;    *newYPos = gyPos;    *newZPos = gzPos;
  64.     if ( (xMove>1) || (xMove<-1) || (yMove>1) || (yMove<-1) || (zMove>1) || (zMove<-1)) {
  65. #if DBG
  66. DebugStr("\p illegal move size");
  67. #endif
  68.         return false;        /* illegal move */
  69.     }
  70.     x = gxPos+xMove;    y = gyPos+yMove;    z = gzPos+zMove;
  71.     if (OutOfBounds(x,y,z)) {
  72. #if DBG
  73. printf("illegal move attempt %ld %ld %ld\n",x,y,z);
  74. #endif
  75.         return false;        /* illegal move */
  76.     }
  77.     if (empty != MazeVal(gMaze,x,y,z)) {
  78.         return false;        /* attempt to move into rock */
  79.     }
  80.     while ( (z>0) && (empty==MazeVal(gMaze,x,y,z-1)) ) --z;    /* fall */
  81.     gxPos = *newXPos = x;    gyPos = *newYPos = y;    gzPos = *newZPos = z;
  82.     result = OnBoundary(x,y,z);
  83.     return result;
  84. }
  85.  
  86. static void SubWide(UnsignedWide *a, UnsignedWide *b, UnsignedWide *diff)
  87. {
  88.     *diff = *b;
  89.     diff->hi -= a->hi;
  90.     diff->lo -= a->lo;
  91.     if (diff->lo > b->lo) --diff->hi;
  92. }
  93.  
  94. int main()
  95. {
  96.     FILE *outFile;
  97.     char *mapStorage;
  98.     long xStart,yStart,zStart;
  99.     UnsignedWide startTime,endTime,diffTime;
  100.     int theFile;
  101.     Boolean solved;
  102.     
  103.     InitGraf(&qd.thePort);
  104.     
  105.     for (theFile=0; theFile<kNumFiles; ++theFile) {
  106.         outFile = fopen(testFiles[theFile],"rb");
  107.         gMaze = ReadMaze(outFile,&gxSize,&gySize,&gzSize,&xStart,&yStart,&zStart);
  108.         
  109.         mapStorage = NewPtrClear(gxSize*gySize*gzSize);
  110.         if (nil==mapStorage) DebugStr("\p storage allocation error");
  111.         
  112.         gxPos = xStart;    gyPos = yStart;    gzPos = zStart;
  113.         
  114.         Microseconds(&startTime);
  115.         solved = Maze(xStart,yStart,zStart,gxSize,gySize,gzSize,MakeAMove,mapStorage);
  116.         Microseconds(&endTime);
  117.         SubWide(&startTime,&endTime,&diffTime);
  118.         
  119.         if (!solved) printf("Entry returned without claiming to solve maze.\n");
  120.         else if (!OnBoundary(gxPos,gyPos,gzPos))
  121.             printf("Entry did not solve maze - %ld %ld %ld not on boundary.\n",gxPos,gyPos,gzPos);
  122.         else printf("Maze exited at x=%ld y=%ld z=%ld\n",gxPos,gyPos,gzPos);
  123.         
  124.         printf("Elapsed time %lu %lu\n",diffTime.hi,diffTime.lo);
  125.         fclose(outFile);
  126.         DisposePtr(mapStorage);
  127.     }
  128.     
  129.     return 0;
  130. }